package com.woniu.search.service.impl;

import com.woniu.cinema.domain.Actor;
import com.woniu.cinema.domain.Cinema;
import com.woniu.cinema.vo.MovieVo;
import com.woniu.common.core.domain.AjaxResult;
import com.woniu.search.entity.ActorSearch;
import com.woniu.search.entity.CinemaSearch;
import com.woniu.search.entity.MovieSearch;
import com.woniu.search.feign.CinemaFeign;
import com.woniu.search.repository.ActorRepository;
import com.woniu.search.repository.CinemaRepository;
import com.woniu.search.repository.MovieRepository;
import com.woniu.search.service.SearchService;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 杨光 微信号:tolryg
 * @time 10:40
 */

@Service
public class SearchServiceImpl implements SearchService
{

    @Autowired
    private CinemaFeign cinemaFeign;

    @Autowired
    private ActorRepository actorRepository;

    @Autowired
    private CinemaRepository cinemaRepository;

    @Autowired
    private MovieRepository movieRepository;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @Override
    public void refreshMovie(Integer movieId){
        List<MovieVo> movieVoList = cinemaFeign.queryMovieVoByMovieId(movieId);
        List<MovieSearch> movieSearchList = movieVoList.stream().map(s -> {
            MovieSearch movieSearch = new MovieSearch();
            BeanUtils.copyProperties(s, movieSearch);
            movieSearch.setMovieTypeStr(s.getMovieTypeStr());
            movieSearch.setSearchText(s.getMovieName() + " " + s.getMovieEname() + " " + s.getMovieInfo());
            return movieSearch;
        }).collect(Collectors.toList());


        movieRepository.saveAll(movieSearchList);
    }


    @RabbitListener(queues = {"search_cinema_queue"})
    @Override
    public void refreshCinema(Integer cinemaId){
        List<Cinema> cinemaList = cinemaFeign.queryCinemaByCinemaId(cinemaId);

        List<CinemaSearch> cinemaSearchList = cinemaList.stream().map(s -> {
            CinemaSearch cinemaSearch = new CinemaSearch();
            BeanUtils.copyProperties(s, cinemaSearch);
            cinemaSearch.setSearchText(s.getCinemaName()+" "+ s.getCinemaLocation());
            return cinemaSearch;
        }).collect(Collectors.toList());


        cinemaRepository.saveAll(cinemaSearchList);

    }

    @Override
    public void refreshActor(Integer actorId){
        List<Actor> actorList = cinemaFeign.queryActorByActorId(actorId);
        List<ActorSearch> actorSearchList = actorList.stream().map(s -> {
            ActorSearch actorSearch = new ActorSearch();
            BeanUtils.copyProperties(s, actorSearch);
            actorSearch.setSearchText(s.getActorName() + "  " + s.getActorDesc());
            return actorSearch;
        }).collect(Collectors.toList());


        actorRepository.saveAll(actorSearchList);
    }

    @Override
    public List<MovieVo> queryMovieVoByMovieId(Integer movieId){
        return cinemaFeign.queryMovieVoByMovieId(movieId);
    }

    @Override
    public List<Cinema> queryCinemaByCinemaId(Integer cinemaId){
        return cinemaFeign.queryCinemaByCinemaId(cinemaId);
    }

    @Override
    public List<Actor> queryActorByActorId(Integer actorId){
        return cinemaFeign.queryActorByActorId(actorId);
    }

    @Override
    public AjaxResult searchMovie(String searchKey) {
        MatchQueryBuilder searchText = QueryBuilders.matchQuery("searchText", searchKey);

        Iterable<MovieSearch> iterable = movieRepository.search(searchText);

        List<MovieSearch> list = new ArrayList<>();

        for (MovieSearch movieSearch : iterable) {

            list.add(movieSearch);
        }

        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult searchActor(String searchKey) {
        MatchQueryBuilder searchText = QueryBuilders.matchQuery("searchText", searchKey);
        Iterable<ActorSearch> iterable = actorRepository.search(searchText);

        List<ActorSearch> list = new ArrayList<>();
        for (ActorSearch actorSearch : iterable) {

            list.add(actorSearch);
        }
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult searchCiname(String searchKey) {
        MatchQueryBuilder searchText = QueryBuilders.matchQuery("searchText", searchKey);
        Iterable<CinemaSearch> iterable = cinemaRepository.search(searchText);
        List<CinemaSearch> list = new ArrayList<>();
        for (CinemaSearch cinemaSearch : iterable) {

            list.add(cinemaSearch);
        }

        return AjaxResult.success(list);
    }
}
